In this lab, you will implement a BST (Binary Search Tree) with Template and the basic operations associated with BST. You will also write a test driver program check your BST implementation.
You need create the following five files for implementing a BST
- Tree Node structure. Class TNode consists of the following members:
Public member:
· parent of type TNode<T>* – pointer to the parent node
· left of type TNode<T>* – it point to the left child
· right of type TNode<T>* – it point to the right
· nodeValue of type T – data value
· default constructor: TNode() – initiate all pointer members to NULL and set nodeValue to T();
Another constructor: TNode(const T&item, TNode<T> *lt = NULL, TNode<T> *rit = NULL, TNode<T> *par = NULL) - set all pointers to the given value by parameters and nodeValue to item
· PrintNodeInfo – member function to print node information (the value of itself, the value of its parent (if the parent is not NULL), the value of its left and right child (if the children are not NULL)
- BSTree (Binary Search Tree) Structure. Class BSTree consists of the following members:
Private member:
· root of type TNode<T>* - it point to the root node of binary search tree
· void insert(TNode<T> * & r, const T & item) - to insert a node with value item into subtree with root r
void del(TNode<T> * & r, const T & item) - to delete a node with value item from subtree with root r
Public member:
· Constructor - initiate Root as a NULL
· Destructor - clean all node in the Binary Search Tree
TNode<T> * getRoot() - to return the value of data member "root"
· BSTInsert (const T & item)- Insert a new node into the binary search tree (For each insert operation. Only distinct value will be inserted. If it is already in the tree, ignore it with some notice.)
· BSTDelete (const T & item) - Delete a node from the binary search tree(For each delete operation, only available element will be deleted from the BST. If there is no such an element to delete, ignore it with some notice.)
- The following free function to traversal and search a binary search tree:
· void InOrder (TNode<T> *r) - for in order walk for a binary search tree with root r
· void PreOrder (TNode<T> *r) - for pre-order walk for a binary search tree with root r
· void PostOrder (TNode<T> *r) - for post-order walk for a binary search tree with root r
· TNode<T> * Search (TNode<T> *r, const T &item) - Search a node with value item in the binary search tree with root r
· TNode<T> * SearchMax (TNode<T> *r) - return the node with the largest value item in the binary search tree with root r
· TNode<T> * SearchMin (TNode<T> *r) - return the node with the smallest value item in the binary search tree with root r
· int countLeaves (TNode<T> * r) - return the number of leaf nodes in the binary search tree with root t
-Write a driver program contains a main function for testing your BST implementation. In the main function, you need to define an empty binary search tree
(e.g., Tnode<int> bt; ) and then allow the following menu options:
1. Insert randomly generated numbers (10 of them) to a binary search tree
2. Traversal the binary search tree using (make sure to print the node information of the node you visited):
(a) Inorder
(b) Postorder
(c) Preoder
4. Search and print a node info
5. Display the smallest value of the tree
6. Display the largest value of the tree
7. Display the number of leaves on the tree
8. Exit the program
Updated: 04/28/2021